package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.beans.IntrospectionException;
import java.lang.invoke.VarHandle;
import java.lang.reflect.*;
import java.security.PublicKey;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zx
 * @date 2021/5/28 22:13
 * @description
 */
public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
        // 调用query方法
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> queryList = simpleExecutor.query(configuration, mappedStatement, params);
        return (List<E>) queryList;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InvocationTargetException, IntrospectionException, InstantiationException {
        List<Object> objects = selectList(statementId, params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("结果为空或者单行查询返回多条");
        }
    }

    @Override
    public int update(String statementId, Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InvocationTargetException, IntrospectionException, InstantiationException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.update(configuration, mappedStatement, params);
    }

    @Override
    public <T> T getMapper(Class<?> tClass) {
        // JDK动态代理
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{tClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 底层执行JDBC 最终是为了判断执行不同的方法
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
                if ("insert".equals(mappedStatement.getMethodType()) || "update".equals(mappedStatement.getMethodType())
                        || "delete".equals(mappedStatement.getMethodType())) {
                    return update(statementId, args);
                } else if ("select".equals(mappedStatement.getMethodType())) {
                    Type genericReturnType = method.getGenericReturnType();
                    if (genericReturnType instanceof ParameterizedType) {
                        List<Object> objects = selectList(statementId, args);
                        return objects;
                    } else {
                        Object o = selectOne(statementId, args);
                        return o;
                    }
                }
                return 0;
            }
        });

        return (T) proxyInstance;
    }
}
